home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS05.ADF / IFF / unpacker.c < prev   
C/C++ Source or Header  |  1986-04-20  |  2KB  |  61 lines

  1.  
  2. /*----------------------------------------------------------------------*
  3.  * unpacker.c Convert data from "cmpByteRun1" run compression. 11/15/85
  4.  *
  5.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  *   control bytes:
  9.  *    [0..127]   : followed by n+1 bytes of data.
  10.  *    [-1..-127] : followed by byte to be repeated (-n)+1 times.
  11.  *    -128       : NOOP.
  12.  *
  13.  * This version for the Commodore-Amiga computer.
  14.  *----------------------------------------------------------------------*/
  15. #include "iff/packer.h"
  16.  
  17.  
  18. /*----------- UnPackRow ------------------------------------------------*/
  19.  
  20. #define UGetByte()  (*source++)
  21. #define UPutByte(c) (*dest++ = (c))
  22.  
  23. /* Given POINTERS to POINTER variables, unpacks one row, updating the source
  24.  * and destination pointers until it produces dstBytes bytes. */
  25. BOOL UnPackRow(pSource, pDest, srcBytes0, dstBytes0)
  26.      BYTE **pSource, **pDest;  WORD srcBytes0, dstBytes0; {
  27.     register BYTE *source = *pSource;
  28.     register BYTE *dest   = *pDest;
  29.     register WORD n;
  30.     register BYTE c;
  31.     register WORD srcBytes = srcBytes0, dstBytes = dstBytes0;
  32.     BOOL error = TRUE;   /* assume error until we make it through the loop */
  33.     WORD minus128 = -128;  /* get the compiler to generate a CMP.W */
  34.  
  35.     while( dstBytes > 0 )  {
  36.      if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  37.      n = UGetByte();
  38.  
  39.      if (n >= 0) {
  40.          n += 1;
  41.          if ( (srcBytes -= n) < 0 )  goto ErrorExit;
  42.          if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  43.          do {  UPutByte(UGetByte());  } while (--n > 0);
  44.          }
  45.  
  46.      else if (n != minus128) {
  47.          n = -n + 1;
  48.          if ( (srcBytes -= 1) < 0 )  goto ErrorExit;
  49.          if ( (dstBytes -= n) < 0 )  goto ErrorExit;
  50.          c = UGetByte();
  51.          do {  UPutByte(c);  } while (--n > 0);
  52.          }
  53.      }
  54.     error = FALSE;  /* success! */
  55.  
  56.   ErrorExit:
  57.     *pSource = source;  *pDest = dest;
  58.     return(error);
  59.     }
  60.  
  61.